<div class="layuimini-container layuimini-page-anim" divname="admin">
    <div class="layuimini-main">

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="search"> <i class="layui-icon"></i> 查询 </button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="refresh"> 刷新 </button>
            </div>
        </script>

        <table class="layui-hide" id="PeoplePage" lay-filter="PeoplePageTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table', 'miniPage', 'element', 'faceAPI'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
        var faceAPI = layui.faceAPI;
        form.render();

        table.render({
            elem: '#PeoplePage',
            //**************************异步请求开始***********************
            url: 'api/People/GetPeopleTable',
            method: 'post',
            headers: {
               // token: 'sasasas',
                Authorization: "Bearer " + faceAPI.GetJWTToken()
            },//增加Token验证头
            request: {
                pageName: 'pageIndex', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            parseData: function (res) { //res 即为原始返回的数据
                if (res.errCode == 0) {
                    var data = res.content;
                    var ret = {
                        "code": res.errCode, //解析接口状态
                        "msg": res.error, //解析提示文本
                        "count": data.totalCount, //解析数据长度
                        "data": data.dataList //解析数据列表
                    };
                    return ret;

                } else {
                    /*layer.open({
                        title: '请求错误',
                        content: '(' + res.errCode + ')' + res.error
                    });  */

                    return {
                        "code": 0, //解析接口状态
                        "msg": '', //解析提示文本
                        "count": 0, //解析数据长度
                        "data": [] //解析数据列表
                    };
                }

            },
            //**************************异步请求结束***********************

            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                { type: "checkbox", width: 50 },
                { field: 'id', width: 70, title: 'ID', sort: true },
                { field: 'employeeID', width: 140, title: '人员编号', sort: true },
                { field: 'employeeName', width: 160, title: '姓名', sort: true },
                {//人员识别凭证
                    field: 'employeeIc', width: 100, title: '凭证', sort: false,
                    templet: function (d) {
                        var sBuf = [];
                        if (d.employeeIc > 0) {
                            sBuf.push('<i class="PeopleAccessKey fa fa-credit-card"  typeTitle="卡号"  type="text"  value = "' + d.employeeIc + '"></i>');
                        }
                        if (d.employeePhoto.length > 0) {
                            sBuf.push('<i class="PeopleAccessKey fa fa-child" typeTitle="照片" type="image" value = "' + d.employeePhoto + '"></i>');
                        }
                        if (d.employeePassword.length > 0) {
                            sBuf.push('<i class="PeopleAccessKey fa fa-at" typeTitle="密码" type="text" value = "' + d.employeePassword + '"></i>');
                        }

                        return sBuf.join(' ');
                    }
                },
                { field: 'employeeJob', title: '职务', width: 160, sort: true },
                { field: 'employeePhone', title: '电话', width: 160, sort: true },
                { title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
            done: function (res, curr, count) {
                //绑定事件
                if (res.count > 0) {
                    //lay-filter="PeoplePageTableFilter"
                    bindPeopleAccessKey();
                }
            }
        });

        function bindPeopleAccessKey() {
            $("[lay-id=PeoplePage] i.PeopleAccessKey").mouseenter(AccessKey_mouseenter);
        }

        faceAPI.PeopleSearchPageResult = null;
        /**
         * toolbar事件监听
         */
        table.on('toolbar(PeoplePageTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('page/people/add.html');
                var openWH = miniPage.getOpenWidthHeight();
                faceAPI.PeopleAddPageResult = null;
                var index = layer.open({
                    title: '添加人员',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function (layero, index) {
                        
                        var rst = faceAPI.PeopleAddPageResult;
                        if (rst != null && rst.result) {
                            //执行搜索重载
                            table.reload('PeoplePage', {
                                page: {
                                    curr: 1
                                }, where: null
                            }, 'data');
                        };

                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('PeoplePage')
                    , data = checkStatus.data;
                var peopleIDs = [];
                //console.log(data);
                data.forEach(function (item, index) {

                    peopleIDs.push(item.id);
                });
                if (peopleIDs.length == 0) {
                    layer.alert('请勾选需要删除的人员ID');
                    return;
                }
                faceAPI.Ajax({
                    type: 'Post',
                    data: { peopleIDs },
                    url: '/api/People/Delete',
                    success: function (result) {
                        if (result.result == false) {
                            layer.alert(result.error, { icon: 2 });
                        }
                        else {
                            //刷新页面
                            table.reload('PeoplePage', {
                                page: {
                                    curr: 1
                                }
                            }, 'data');
                        }

                    }
                });
                layer.alert(JSON.stringify(userids));
            }
            else if (obj.event === 'search') {  // 查询操作
                var content = miniPage.getHrefContent('page/people/search.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '搜索人员',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function (layero, index) {

                        var rst = faceAPI.PeopleSearchPageResult;
                        if (rst != null && rst.result) {
                            console.log("刷新")
                            //执行搜索重载
                            table.reload('PeoplePage', {
                                page: {
                                    curr: 1
                                }
                                , where: rst.field
                            }, 'data');

                        };

                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            else if (obj.event == 'refresh') {
                //执行搜索重载
                faceAPI.PeopleSearchPageResult = null;

                table.reload('PeoplePage', {
                    page: {
                        curr: 1
                    }, where: null
                }, 'data');
            }
        });


        table.on('tool(PeoplePageTableFilter)', function (obj) {
            var data = obj.data;

            if (obj.event === 'edit') {

                var content = miniPage.getHrefContent('page/people/edit.html');
                var openWH = miniPage.getOpenWidthHeight();
                faceAPI.PeopleEditPage = data;//向layer页面传值，传值主要代码
                faceAPI.PeopleEditPageResult = null;
                var index = layer.open({
                    title: '编辑人员',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function (layero, index) {

                        //console.log(ImageUtility());

                        var rst = faceAPI.PeopleEditPageResult;
                        console.log(rst);
                        if (rst != null && rst.result) {
                            var newuser = rst.content;
                            //执行搜索重载

                            obj.update(newuser);
                            bindPeopleAccessKey();
                        };

                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
            else if (obj.event === 'delete') {

                layer.confirm('操作确认：删除人员[' + data.employeeName + ']?', function (index) {
                    var peopleIDs = [];
                    peopleIDs.push(data.id);
                    faceAPI.Ajax({
                        type: 'Post',
                        data: { peopleIDs },
                        url: '/api/People/Delete',
                        success: function (result) {
                            if (result.result == false) {
                                layer.alert(result.error, { icon: 2 });
                            }
                            else {
                                //刷新页面
                                obj.del();
                            }

                        }
                    });


                    layer.close(index);
                });
            }
        });

        table.on('sort(PeoplePageTableFilter)', function (obj) {
            bindPeopleAccessKey();

        });

        // 在线离线状态显示提示框
        var iMouseenter_TipIndex = 0;
        function AccessKey_mouseenter() {
            var that = this;
            if (iMouseenter_TipIndex > 0)
                layer.close(iMouseenter_TipIndex);
            var spn = $(that);
            var sType = spn.attr("type");
            var stypeTitle = spn.attr("typeTitle");
            var sValue = spn.attr("value");
            var sHtml = '';//'<div class="layui-card">';
            //sHtml = sHtml + '<div class="layui-card-header">' + stypeTitle + '</div>';
            //sHtml = sHtml + '<div class="layui-card-body">';



            var par = { area: ['240px', '160px'] };
            //debugger;
            if (sType == "text") {

                sHtml = '<h2>' + stypeTitle + '</h2>' + '<h3>' + sValue + '</h3>';
            }
            else {
                par.area[0] = '260px';
                par.area[1] = '380px';
                sHtml = '<h2>' + stypeTitle + '</h2>' + '<img id="showPeopleImg" src="' + sValue + '" width="240"/>';
            }


            //sHtml = sHtml + '</div></div>';

            //console.log(sHtml);
            iMouseenter_TipIndex = layer.tips(sHtml, that, par);
        }


    });



</script>